package com.zrx.app.android.fragment;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;

import com.alibaba.fastjson.JSONObject;
import com.zrx.app.android.R;
import com.zrx.app.android.UTIL.ZRXUTIL;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by liuji on 2017/4/14.
 *
 *
 * ？？？
 */
@SuppressLint("ValidFragment")
public class ItemFrag extends BaseFragment {

    private static final String TAG = "RootFrag";
    JSONObject node, savedJson;
    String key,dir,savePath;

    public ItemFrag(JSONObject node,JSONObject savedJson,String key,String dir,String savePath) {
        this.node = node;
        this.savedJson = savedJson;
        this.savePath = savePath;
        this.key = key;
        this.dir=dir;
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup vg,
                             Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_item, vg, false);

        final GridLayout ll = (GridLayout) view.findViewById(R.id.dyn_content);
//        if("产品信息".equals(key)||"购车信息".equals(key)||"基本信息".equals(key)||"参考资料".equals(key)||"附加信息".equals(key)){
//            ll.setColumnCount(1);
//        }
        if(node!=null&&node.containsKey("column")){
            int column=node.getInteger("column");
            ll.setColumnCount(column);
        }


        if(node==null){
            Log.d(TAG,"node==null");
            return view;
        }

        try{
            final List<String> orderedList = ZRXUTIL.sortByOrder(node.keySet(), node);

            for (String key : orderedList) {

                Object obj = node.get(key);

                BaseFragment myFrag = null;

                if (obj instanceof JSONObject) {
                    JSONObject innerNode = (JSONObject) obj;
                    String type = innerNode.getString("type");
                    if ("字段".equals(type)) {
                        myFrag = new TextFrag();
                    } else if ("照片".equals(type)) {
                        if (innerNode.containsKey("appendable") && innerNode.getBoolean("appendable").equals(true)) {
                            myFrag = new ImageAppendableFrag();
                        } else {
                            myFrag = new ImageFrag();
                        }
                    } else if ("视频".equals(type)) {
                        myFrag = new VideoFrag();
                    } else
                        Log.d(this.getClass().getName(), key);

                    if (myFrag != null)
                    {
                        myFrag.initParams(innerNode,key,dir);
                        FragmentManager fragMan = getChildFragmentManager();
                        FragmentTransaction fragTransaction = fragMan.beginTransaction();

                        fragTransaction.add(ll.getId(), myFrag);
                        fragTransaction.commit();
                    }
                }


            }

            if(node.containsKey("appendable")&&node.getBoolean("appendable").equals(true)){
                AdditionalDataFrag addFrag=new AdditionalDataFrag();
                addFrag.initParams(node,key,dir);

                FragmentManager fragMan = getChildFragmentManager();
                FragmentTransaction fragTransaction = fragMan.beginTransaction();

                fragTransaction.add(ll.getId(), addFrag);
                fragTransaction.commit();
            }

            view.scrollTo(0, 0);
        }catch (Exception e){
            Log.d(TAG,e.getMessage());
        }
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
//        File file = new File(dir + "/obj.json");
        File file = new File(savePath);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write(savedJson.toJSONString().getBytes());
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
